package com.panda.web.utils.DeepSeek;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class ResumeGenerator {
    public static void generateResume(String templatePath, String outputPath, Map<String, String> dataMap, String photoPath) {
        try {
            // 读取模板文件
            FileInputStream templateStream = new FileInputStream(templatePath);
            XWPFDocument doc = new XWPFDocument(templateStream);

            // 替换模板中的占位符
            for (XWPFParagraph paragraph : doc.getParagraphs()) {
                for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                    String placeholder = "{{" + entry.getKey() + "}}";
                    String replacement = entry.getValue();
                    for (XWPFRun run : paragraph.getRuns()) {
                        String text = run.getText(0);
                        if (text != null && text.contains(placeholder)) {
                            text = text.replace(placeholder, replacement);
                            run.setText(text, 0);
                        }
                    }
                }
            }


            // 输出到新的文件
            FileOutputStream outputStream = new FileOutputStream(outputPath);
            doc.write(outputStream);

            // 关闭流
            templateStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//    public static void generateResume(String templatePath, String outputPath, Map<String, String> data, String photoPath) throws IOException {
//        try (FileInputStream fis = new FileInputStream(templatePath);
//             XWPFDocument document = new XWPFDocument(fis)) {
//
//            // 先插入照片
//            insertPhoto(document, photoPath);
//
//            // 替换段落中的占位符
//            for (XWPFParagraph paragraph : document.getParagraphs()) {
//                replacePlaceholdersInParagraph(paragraph, data);
//            }
//
//            // 替换表格中的占位符
//            for (XWPFTable table : document.getTables()) {
//                replacePlaceholdersInTable(table, data);
//            }
//
//            // 保存替换后的文档
//            try (FileOutputStream fos = new FileOutputStream(outputPath)) {
//                document.write(fos);
//            }
//        } catch (InvalidFormatException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    private static void replacePlaceholdersInParagraph(XWPFParagraph paragraph, Map<String, String> data) {
//        for (XWPFRun run : paragraph.getRuns()) {
//            String text = run.getText(0);
//            if (text != null) {
//                // 替换所有占位符（除了 ${photo}）
//                for (Map.Entry<String, String> entry : data.entrySet()) {
//                    String placeholder = "${" + entry.getKey() + "}";
//                    if (text.contains(placeholder)) {
//                        // 如果数据存在，替换占位符；否则替换为“未提供”
//                        String value = entry.getValue() != null ? entry.getValue() : "未提供";
//                        text = text.replace(placeholder, value);
//                        run.setText(text, 0);
//                    }
//                }
//
//                // 处理未赋值的占位符（除了 ${photo}）
//                text = replaceUnassignedPlaceholders(text);
//                run.setText(text, 0);
//            }
//        }
//    }
//
//    private static void replacePlaceholdersInTable(XWPFTable table, Map<String, String> data) {
//        for (XWPFTableRow row : table.getRows()) {
//            for (XWPFTableCell cell : row.getTableCells()) {
//                for (XWPFParagraph paragraph : cell.getParagraphs()) {
//                    replacePlaceholdersInParagraph(paragraph, data);
//                }
//            }
//        }
//    }
//
//    private static void insertPhoto(XWPFDocument document, String photoPath) throws IOException, InvalidFormatException {
//        System.out.println("Searching for ${photo} placeholder...");
//
//        // 检查段落
//        for (XWPFParagraph paragraph : document.getParagraphs()) {
//            for (XWPFRun run : paragraph.getRuns()) {
//                String text = run.getText(0);
//                if (text != null && text.contains("${photo}")) {
//                    System.out.println("Found ${photo} placeholder in paragraph. Replacing with image...");
//                    // 删除占位符
//                    run.setText("", 0);
//
//                    // 插入图片
//                    try (FileInputStream photoStream = new FileInputStream(photoPath)) {
//                        String extension = photoPath.substring(photoPath.lastIndexOf(".") + 1).toLowerCase();
//                        int pictureType;
//                        switch (extension) {
//                            case "jpg":
//                            case "jpeg":
//                                pictureType = XWPFDocument.PICTURE_TYPE_JPEG;
//                                break;
//                            case "png":
//                                pictureType = XWPFDocument.PICTURE_TYPE_PNG;
//                                break;
//                            case "gif":
//                                pictureType = XWPFDocument.PICTURE_TYPE_GIF;
//                                break;
//                            default:
//                                throw new IllegalArgumentException("Unsupported image format: " + extension);
//                        }
//                        run.addPicture(photoStream, pictureType, photoPath, Units.toEMU(100), Units.toEMU(100));
//                        System.out.println("Image inserted successfully in paragraph.");
//                    }
//                }
//            }
//        }
//
//        // 检查表格
//        for (XWPFTable table : document.getTables()) {
//            for (XWPFTableRow row : table.getRows()) {
//                for (XWPFTableCell cell : row.getTableCells()) {
//                    for (XWPFParagraph paragraph : cell.getParagraphs()) {
//                        for (XWPFRun run : paragraph.getRuns()) {
//                            String text = run.getText(0);
//                            if (text != null && text.contains("${photo}")) {
//                                System.out.println("Found ${photo} placeholder in table cell. Replacing with image...");
//                                // 删除占位符
//                                run.setText("", 0);
//
//                                // 插入图片
//                                try (FileInputStream photoStream = new FileInputStream(photoPath)) {
//                                    String extension = photoPath.substring(photoPath.lastIndexOf(".") + 1).toLowerCase();
//                                    int pictureType;
//                                    switch (extension) {
//                                        case "jpg":
//                                        case "jpeg":
//                                            pictureType = XWPFDocument.PICTURE_TYPE_JPEG;
//                                            break;
//                                        case "png":
//                                            pictureType = XWPFDocument.PICTURE_TYPE_PNG;
//                                            break;
//                                        case "gif":
//                                            pictureType = XWPFDocument.PICTURE_TYPE_GIF;
//                                            break;
//                                        default:
//                                            throw new IllegalArgumentException("Unsupported image format: " + extension);
//                                    }
//                                    run.addPicture(photoStream, pictureType, photoPath, Units.toEMU(100), Units.toEMU(100));
//                                    System.out.println("Image inserted successfully in table cell.");
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    /**
//     * 替换未赋值的占位符为默认文字（排除 ${photo}）
//     */
//    private static String replaceUnassignedPlaceholders(String text) {
//        // 定义需要替换的占位符正则表达式（排除 ${photo}）
//        String placeholderPattern = "\\$\\{(?!photo\\b)[^}]+\\}";
//        // 替换为默认文字
//        return text.replaceAll(placeholderPattern, "未提供");
//    }
//}